# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=gts
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.6
pkgrel=1
pkgdesc="Provides useful functions to deal with 3D surfaces meshed with interconnected triangles (mingw-w64)"
arch=('any')
url="http://gts.sourceforge.net/"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-glib2")
source=("https://downloads.sourceforge.net/project/${_realname}/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "gts-mingw.patch")
sha256sums=('059c3e13e3e3b796d775ec9f96abdce8f2b3b5144df8514eda0cc12e13e8b81e'
            '7efb4a8e76dbb9b18bd35b7021ed269d8d5c871b3acd22367c4febf27d67fed7')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/gts-mingw.patch"
  WANT_AUTOMAKE=latest autoreconf -fiv
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/${_realname}-${pkgver}/. "${srcdir}"/build-${CARCH}
  cd "${srcdir}"/build-${CARCH}
  
  export LDFLAGS=${LDFLAGS//--as-needed,}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
  
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gts-config"
  
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
